#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char *argv[])
{
        struct sockaddr_in servaddr, client_addr;
        int sockfd, source_fd;
        char hostname[1024], iobuffer[1024];
        struct hostent *phost = NULL;
	char filename[1024], segment_path[1024];
	int j,num, ret, size = 0;
	char *pname = NULL;
	char path[1024];
        if (argc < 2)
        {
                printf("Usage: %s path min max\n", argv[0]);
                return -1;
        }

        sockfd = socket( AF_INET, SOCK_STREAM, 0);
        if ( sockfd < 0)
        {
                printf("socket err\n");
                return -1;
        }
	
	bzero(&client_addr, sizeof(client_addr));
	client_addr.sin_family = AF_INET;
	client_addr.sin_addr.s_addr = htons(INADDR_ANY);
	client_addr.sin_port = htons(0);
	/*
	if ( bind( sockfd, (struct sockaddr*)&client_addr, sizeof(client_addr)))
	{
		printf("bind err %d \n", errno);
		return -1;
	}
	
	*/
        servaddr.sin_family = AF_INET;
        inet_aton( "127.0.0.1", &servaddr.sin_addr);
	//servaddr.sin_addr.s_addr = inet_aton( "127.0.0.1" );
        servaddr.sin_port = htons(36600);

        //send to server by hostname
        gethostname(hostname, 1024);
        printf("host name %s\n", hostname);

        //phost = gethostbyname(hostname);
	//phost = gethostbyname("computer-2-2.local");
        
	//printf("gate addr = %s\n", inet_ntoa( *(struct in_addr*)phost->h_addr_list[0]));
        //servaddr.sin_addr = (*(struct in_addr*)phost->h_addr_list[0]);

        if ( connect( sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
        {
                printf("Connect err: %d\n", errno);
                printf("sin_family = %d, sin_port = %d, sin_addr = %s, \n", servaddr.sin_family, servaddr.sin_port, inet_ntoa(servaddr.sin_addr));
		return -1;
        }
	
	getcwd(path, 1024);
	printf("getcwd path = %s\n", path);
	pname = argv[0] + 2;
	sprintf(filename, "%s/%s", path, pname);
	printf("filename is %s, and pname is %s\n", filename, pname);
        //strcpy(filename, argv[1]);
	
        if ((source_fd = open(filename, O_RDONLY, 0) ) == -1)
        {
                printf("file open err\n");
                return -1;
        }
	printf("source_fd = %d\n", source_fd);
	sprintf(segment_path, "%s/%s", argv[1], pname);
	printf("segment_path: %s\n", segment_path);
        ret = write(sockfd, segment_path, sizeof(segment_path) );
	if ( -1 == ret)
	{
		printf("write err %d\n", errno);
		return -1;
	}
	printf("write ret = %d\n", ret);
	while ( (num = read(source_fd, iobuffer, sizeof(iobuffer))) > 0)
        {
		printf("num = %d\n",  num);
		/*
		for ( j = 0; j<num; j++)
		{
			printf("data=%x", iobuffer[j]);
		}
		*/
                write(sockfd, iobuffer, num);
		size += num;
        }
	printf("\nsent data: %6d bytes\n", size);
	close(source_fd);
        close(sockfd);
	printf("send file success\n");

        return 0;



}
                                                       
